// addContact.js

var common = require("../../../common/utils.js");
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appColor: app.appColor,
    show: false, //控制下拉列表的显示隐藏，false隐藏、true显示
    array: ['身份证', '回乡证', '护照', '台胞证'], //下拉列表的数据
    index: 0,
    inputType: 'idcard'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.carid && options.name) {
      this.setData({
        userCarid: options.carid,
        userName: options.name,
        takemanid: options.takemanid,
        userPhone: options.phone == 'null' || options.phone == 'undefined' ? '' : options.phone
      })
    }
  },

  bindPickerChange: function(e) {
    let val = e.detail.value;
    this.setData({
      index: val,
      inputType: val == '0' ? 'idcard' : 'text'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  // 获取input值
  userName: function(e) {
    this.setData({
      TakemanNames: e.detail.value
    })
  },
  cardId: function(e) {
    this.setData({
      TakemanCardIDs: e.detail.value
    })
  },
  phoneFun: function(e) {
    this.setData({
      userPhone: e.detail.value
    })
  },

  // 提示框
  repetition: function(message) {
    wx.showModal({
      content: message,
      showCancel: false,
      success: function(res) {
        if (res.confirm) {
          console.log('确定')
        }
      }
    });
  },

  // 删除用户
  deleteUser: function(takemanid, TakemanName, TakemanCardID, TakemanPhone) {
    let that = this;
    wx.request({
      url: app.serviceurl + 'Member/DeleteTakeman',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        takemanid: takemanid
      },
      dataType: 'json',
      success: function(res) {
        if (!res.data.Result) {
          that.repetition('添加失败');
          return
        }
        that.amend(TakemanName, TakemanCardID, TakemanPhone);

      },
      fail: function(error) {
        console.log(error);
      }
    })
  },

  // 添加新用户
  amend: function(TakemanName, TakemanCardID, TakemanPhone) {
    let that = this;
    wx.request({
      url: app.serviceurl + 'Member/AppendTakeman',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        TakemanName: TakemanName,
        TakemanCardID: TakemanCardID,
        TakemanPone: TakemanPhone ? TakemanPhone : "",
        IdType: Number(that.data.index) + 1
      },
      dataType: 'json',
      success: function(res) {
        if (res.data.Result == false) {
          that.repetition(res.data.Message);
          return
        } else {
          wx.navigateBack({});
        }
      },
      fail: function(error) {
        that.repetition('添加失败');
      }
    });
  },

  save: function() {
    try {
      let phone = this.data.userPhone;
      // 如果用户输入手机号进行判断
      if (typeof (phone) !== 'undefined' && phone !== '') {
        if (!common.ChkUtil.isMobile(phone)) {
          this.repetition("请核实手机号是否正确");
          return
        }
      }
      // // 姓名验证 --- 1-20字符
      // let RegName = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{1,20}$/;
      // 除身份证外，其它证件号的正则表达式并不完善，后台也有验证
      let hui = /^([A-Z]\d{6,11}(\(\w{1}\))?)$/;
      let tai = /^\d{8}|^\d{18}|^[a-zA-Z0-9]{10}$/;
      let hu = /^([a-zA-z]|[0-9]){5,17}$/;
      var TakemanName;
      var TakemanCardID;
      if (!this.data.TakemanCardIDs && !this.data.TakemanNames) {
        // 去空格
        TakemanName = this.data.userName.replace(/\s/g, "");
        TakemanCardID = this.data.userCarid.replace(/\s/g, "");
      } else {
        // 去空格
        TakemanName = this.data.TakemanNames ? this.data.TakemanNames.replace(/\s/g, "") : this.data.userName.replace(/\s/g, "");
        TakemanCardID = this.data.TakemanCardIDs ? this.data.TakemanCardIDs.replace(/\s/g, "") : this.data.userCarid.replace(/\s/g, "");
      }
      if (!TakemanCardID || !TakemanName) {
        this.repetition("请核实信息是否填正确");
        return
      }
      // 回乡证判断
      if (this.data.index == 1) {
        if (hui.test(TakemanCardID)) {
          this.amend(TakemanName, TakemanCardID, phone)
          return
        } else {
          this.repetition("请核对证件号是否正确");
        }
        return
      }
      // 护照判断
      if (this.data.index == 2) {
        if (hu.test(TakemanCardID)) {
          this.amend(TakemanName, TakemanCardID, phone)
          return
        } else {
          this.repetition("请核对证件号是否正确");
        }
        return
      }
      // 台胞证判断
      if (this.data.index == 3 && tai.test(TakemanCardID)) {
        if (tai.test(TakemanCardID)) {
          this.amend(TakemanName, TakemanCardID, phone)
          return
        } else {
          this.repetition("请核对证件号是否正确");
        }
        return
      }

      if (TakemanName && common.ChkUtil.isIdCardNew(TakemanCardID)) {
        // 所有验证通过
        if (this.data.takemanid) {
          this.deleteUser(this.data.takemanid, TakemanName, TakemanCardID, phone)
        } else {
          this.amend(TakemanName, TakemanCardID, phone)
        }
      } else if (!TakemanName) {
        this.repetition("姓名需要2个中文以上")
      } else if (!common.ChkUtil.isIdCardNew(TakemanCardID)) {
        this.repetition("请确认这是正确的证件号")
      }
    } catch (err) {
      this.repetition('添加失败');
      console.log(err)
    }
  }


})